/*
 * Copyright (C) 2010 mAPPn.Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.mol.market.common.widget;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

import com.mol.market.Constants;
import com.mol.market.R;
import com.mol.market.common.util.ImageUtils;

/**
 * 禁止快速滑动的Gallery
 * 
 * @author andrew.wang
 * @date 2012-1-9
 * @since Version 0.9
 */
public class SlowGallery extends Gallery {

    public SlowGallery(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SlowGallery(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SlowGallery(Context context) {
        super(context);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return super.onFling(e1, e2, velocityX / 3, velocityY);
    }
    
    static class TopRecommendAdapter extends BaseAdapter {

        private Context mContext;
        private ArrayList<HashMap<String, Object>> mDataSource;
        private LayoutInflater mLayoutInflater;

        public TopRecommendAdapter(Context context, ArrayList<HashMap<String, Object>> data) {
            mContext = context;
            mDataSource = data;
            mLayoutInflater = LayoutInflater.from(mContext);
        }

        @Override
        public int getCount() {
            if (mDataSource == null || mDataSource.size() == 0) {
                return 0;
            }
            return mDataSource.size();
        }

        public void setData(ArrayList<HashMap<String, Object>> data) {
            if (mDataSource == null) {
                mDataSource = new ArrayList<HashMap<String, Object>>();
            }
            mDataSource.addAll(data);
            notifyDataSetChanged();
        }

        @Override
        public Object getItem(int position) {
            return mDataSource.get(position);
        }

        @Override
        public long getItemId(int arg0) {
            return arg0;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ImageView v = null;
            if (convertView == null) {
                v = (ImageView) mLayoutInflater.inflate(R.layout.home_gallery_item, parent, false);
            } else {
                v = (ImageView) convertView;
            }
            Drawable old = v.getDrawable();
            v.setImageDrawable(null);
            if (old != null) {
                old.setCallback(null);
            }
            
            Object picUri = mDataSource.get(position).get(Constants.KEY_PIC_URL);
            if (picUri instanceof String) {
                ImageUtils.downloadHomeTopDrawable(mContext, (String) picUri, v);
            } else if (picUri instanceof Integer) {
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inPurgeable = true;
                o.inInputShareable = true;
                Bitmap bmp = BitmapFactory
                        .decodeResource(mContext.getResources(), (Integer) picUri, o);
                Drawable d = ImageUtils.getMaskDrawable(mContext);
                v.setImageDrawable(d);
                v.setBackgroundDrawable(new BitmapDrawable(bmp));
            }
            
            return v;
        }
    }

}
